// pages/findowerns/findowerns.js
//引入请求代码
var call = require("../../utils/ajax.js")
var that;
let ol = 0;
// var ImgURL = "";
var app = getApp();
var petSelectList = require("../../utils/petSelectList.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        URL:"",
        FileName1: "",
        brands: [],
        objectArray: [{
                brand: "福州",
                id: 0,
                array: ['鼓楼区', '台江区', '仓山区', '马尾区', '晋安区', '福清市', '长乐市', '闽侯县', '连江县', '罗源县', '闽清县', '永泰县']
            },
            {
                brand: "厦门",
                id: 1,
                array: ['思明区', '海沧区', '湖里区', '集美区', '同安区', '翔安区']
            },
            {
                brand: "漳州",
                id: 2,
                array: ['芗城区', '龙文区', '龙海市', '漳浦县', '南靖县', '云霄县', '平和县', '华安县', '长泰县', '诏安县', '东山县']
            },
            {
                brand: "龙岩",
                id: 3,
                array: ['新罗区', '漳平市', '长汀县', '永定县', '上杭县', '武平县', '连城县']
            },
            {
                brand: "宁德",
                id: 4,
                array: ['蕉城区', '东侨新区', '福安市', '福鼎市', '霞浦县', '古田县', '屏南县', '寿宁县', '周宁县', '柘荣县']
            },
            {
                brand: "平潭",
                id: 5,
                array: ['潭城', '苏澳', '流水', '澳前', '北厝', '平原', '敖东']
            },
            {
                brand: "泉州",
                id: 6,
                array: ['丰泽区', '鲤城区', '洛江区', '泉港区', '石狮市', '晋江市', '南安市', '惠安县', '安溪县', '永春县', '德化县', '金门县']
            },
            {
                brand: "三明",
                id: 7,
                array: ['梅列区', '三元区', '永安市', '明溪县', '清流县', '宁化县', '大田县', '尤溪县', '沙县', '将乐县', '泰宁县', '建宁县']
            },
            {
                brand: "莆田",
                id: 8,
                array: ['城厢区', '涵江区', '荔城区', '秀屿区', '仙游县']
            },
            {
                brand: "南平",
                id: 8,
                array: ['延平区', '邵武市', '武夷山市', '建瓯市', '建阳市', '顺昌县', '浦城县', '光泽县', '松溪县', '政和县']
            }
        ],
        object: [],
        brandindex: 0,
        index1: 0,
        currentData: 0,
        dates: '2019-10-01',
        times: '12:00',
        catList: [],
        doglList: [],
        petItems: [{
                name: '喵星人',
                value: '喵星人',
            },
            {
                name: '汪星人',
                value: '汪星人',
            }
        ],
        sexItems: [{
                name: '男孩子',
                value: '男孩子',
            },
            {
                name: '女孩子',
                value: '女孩子',
            },
        ],
        images: [],
        uploadedImages: [],
        ImgURL: "",
        search_pet_name: "",
        search_pet_age: "",
        // search_pet_contact: "",
        search_pet_message: "",
        search_pet_area: "",
        publisher_phone: "",
        lose_city_area: "",
        search_pet_sex: "",
        lose_county_area: "",
        lose_detailed_area: "",
        special_advice: "",
        kk: ""
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
                this.setData({
                        URL:app.globalData.URL
                    })
            if(!app.globalData.userState){
                wx.showToast({
                    title: '您未登录！',
                    icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
                    duration: 2000     
                  }) 
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1, // 回退前 delta(默认为1) 页面
                    })
                }, 1000)
            }
        ol = 0;
        let ha = options.pet_id;
        let that = this;
        let catList = petSelectList.catList();
        let doglList = petSelectList.doglList();
        this.setData({
            catList: catList,
            doglList: doglList
        })
        var objectArray = this.data.objectArray
        var brands = []
        for (var i = 0; i < objectArray.length; i++) {
            brands.push(objectArray[i].brand)
        }
        this.setData({
            brands: brands,
            object: objectArray[this.data.brandindex].array,
            lose_city_area: objectArray[this.data.brandindex].brand,
            lose_county_area: this.data.objectArray[this.data.brandindex].array[this.data.index1]
        })

        let url = "SearchPet/selectOneSearchPet/" + ha
        call.getData1(url, function(res) {
            console.log(res)
                //加入宠物id
            res.date.search_pet_id = ha;
            let pet_id = res.date;
            that.setData({
                petmessage: pet_id,
                Image: res.date.imgUrls
            })

            //此处加载其他的信息
            //开始切割分开日期和时间
            let date = res.date.lose_time.split(" ")[0];
            let time = res.date.lose_time.split(" ")[1];
            console.log(date)
            that.setData({
                search_pet_name: res.date.search_pet_name,
                search_pet_message: res.date.search_pet_message,
                publisher_phone: res.date.publisher_phone,
                lose_detailed_area: res.date.lose_detailed_area,
                FileName1: res.date.file_name,
                dates: date,
                times: time,
                search_pet_age: res.date.search_pet_age,
                special_advice: res.date.special_advice
            });
            //加载性别和种类、品种
            let sex = res.date.search_pet_sex;
            that.admin2(sex)
            if (res.date.search_pet_category == "汪星人") {
                console.log("汪星人")
                let doglList1 = doglList;
                let numH = null;
                let pet = res.date.search_pet_varieties;
                console.log(pet);
                for (var i = 0; i <= doglList1.length; i++) {
                    let kk = "";
                    try {
                        kk = doglList1[i]['name'];
                    } catch (e) {
                        console.log(e)
                    }
                    if (kk != undefined && kk != "") {
                        if (pet === doglList1[i]['name']) {
                            numH = i;
                        }
                    }

                }
                let petItems = that.data.petItems; //选项集合
                petItems[1].checked = true; //改变当前选中的checked值
                that.setData({
                    petItems: petItems,
                });
                that.setData({
                    value: that.data.doglList[numH].name,
                    Index: numH,
                    hideAdd: 1
                })
            }

            if (res.date.search_pet_category == "喵星人") {
                let catList1 = catList;
                let numH = null;
                let pet = res.date.search_pet_varieties;
                console.log(pet);
                for (var i = 0; i <= catList1.length; i++) {
                    let kk = "";
                    try {
                        kk = catList1[i]['name'];
                    } catch (e) {
                        console.log(e)
                    }
                    if (kk != undefined && kk != "") {
                        if (pet === catList1[i]['name']) {
                            numH = i;
                        }
                    }
                }
                let petItems = that.data.petItems; //选项集合
                petItems[0].checked = true; //改变当前选中的checked值
                that.setData({
                    petItems: petItems,
                });
                console.log(that.data.catList[numH])
                that.setData({
                    value: that.data.catList[numH].name,
                    Index: numH,
                    hideAdd: 0
                })
            }
            //地区循环输出修改的东西
            let objectArray1 = that.data.objectArray
            let city = res.date.lose_city_area;
            let county = res.date.lose_county_area;
            for (var i = 0; i < objectArray1.length; i++) {
                if (city == objectArray1[i].brand) {
                    that.setData({
                        brandindex: i,
                        object: objectArray[i].array,
                    })
                }
                for (var j = 0; j < objectArray1[i].array.length; j++) {
                    if (county == objectArray1[i].array[j]) {

                        that.setData({
                            index1: j,
                        })
                    }
                }
            }

            //此处单独加载图片的数据
            let imgList = [];
            imgList = res.date.imgUrls;
            console.log(imgList)
            that.setData({
                    images: imgList
                })
                //让图片的占位,并且判断是否隐藏加图片
            ol = that.data.images.length;
            let ten = false;
            console.log(ol)
            if (ol >= 3) {
                console.log("ss")
                ten = true
            } else {
                console.log("ss")
                ten = false
            }
            that.setData({
                kk: ten
            });


        })



    },
    admin2(value) {
        let num1 = null;

        function admin3(value) {
            switch (value) {
                case "男孩子":
                    console.log(0);
                    num1 = 0;
                    break;
                case "女孩子":
                    console.log(1)
                    num1 = 1;
                    break;
                    // default:
                    //    默认代码块
            }
        }
        var sexItems = this.data.sexItems;
        admin3(value);
        sexItems[num1].checked = true; //改变当前选中的checked值
        this.setData({
            sexItems: sexItems
        })

    },
    bindPickerChangecity: function(e) {
        this.setData({
            brandindex: e.detail.value,
            index1: 0
        })
        var objectArray = this.data.objectArray
        this.setData({
            object: objectArray[this.data.brandindex].array
        })

        // console.log(this.data.brands[this.data.brandindex])
        this.setData({
            lose_city_area: this.data.brands[this.data.brandindex]
        })
        console.log(this.data.lose_city_area)

        // console.log(this.data.brandindex)
        //     console.log(objectArray[this.data.brandindex].array[this.data.index1])
    },
    bindPickerChangearea: function(e) {
        this.setData({
            index1: e.detail.value
        })
        this.setData({
                lose_county_area: this.data.objectArray[this.data.brandindex].array[this.data.index1]
            })
            // console.log(this.data.objectArray[this.data.brandindex].array[this.data.index1])
        console.log(this.data.lose_county_area)
    },

    bindTimeChange: function(e) {
        console.log(e.detail.value)
        this.setData({
            times: e.detail.value
        })
    },
    //  点击日期组件确定事件  
    bindDateChange: function(e) {
        console.log(e.detail.value)
        this.setData({
            dates: e.detail.value
        })
    },

    radio: function(e) {

        var index = e.currentTarget.dataset.index; //获取当前点击的下标
        var petItems = this.data.petItems; //选项集合
        var sexItems = this.data.sexItems;
        if (petItems[index].checked) return; //如果点击的当前已选中则返回
        petItems.forEach(item => {
            item.checked = false
        })
        petItems[index].checked = true; //改变当前选中的checked值
        this.setData({
            petItems: petItems
        });


    },
    bindPickerChange: function(e) {
        var that = this;
        that.setData({
            value: that.data.catList[e.detail.value].name,
            Index: e.detail.value
        })
        console.log('picker发送选择改变，携带值为', that.data.catList[e.detail.value].name)
    },
    bindPickerChange1: function(e) {
        var that = this;
        that.setData({

            value: that.data.doglList[e.detail.value].name,
            Index: e.detail.value
        })
        console.log('picker发送选择改变，携带值为', that.data.doglList[e.detail.value].name)
    },
    radio_sex: function(e) {
        var index = e.currentTarget.dataset.index;
        var sexItems = this.data.sexItems;
        if (sexItems[index].checked) return; //如果点击的当前已选中则返回
        sexItems.forEach(item => {
            item.checked = false
        })
        sexItems[index].checked = true; //改变当前选中的checked值
        this.setData({
            sexItems: sexItems
        });
    },

    radioChange: function(e) {
        var checkValue = e.detail.value;
        this.setData({
            checkValue: checkValue
        });
        console.log(checkValue)
        var that = this
        if (checkValue == '喵星人') {
            that.setData({
                hideAdd: 0
            })
        } else {
            that.setData({
                hideAdd: 1
            })
        }

    },
    radioChange_sex: function(e) {
        var checkValue_sex = e.detail.value;
        this.setData({
            checkValue_sex: checkValue_sex
        });
        console.log(checkValue_sex)
    },
    sendImg: function(imgurl) {


    },
    chooseImage: function() {
        // 选择图片
        var that = this
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths[0];
                let kla = false;

                function admin(tempFilePaths) {
                    console.log("ss");
                    ol = ol + 1;
                    if (ol >= 3) {
                        console.log("ss")
                        kla = true
                    } else {
                        console.log("ss")
                        kla = false
                    }
                    return tempFilePaths;
                }
                let ddd = admin(tempFilePaths);
                //修改上传其他的图片
                let FileName = that.data.FileName1;
                let kk = that.data.images;
                let url = 'Img/addImg?className=searchPet&fileName=' + FileName;
                let data = {
                    URL: url, //这里是你要上传的服务器接口
                    path: ddd, //这里是你最开始定义的图片数组
                    success: function(ress) {
                        console.log(ress)
                        if (ress.message == "上传成功") {
                            wx.showToast({
                                title: '上传成功',
                                icon: '', //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
                                duration: 2000, //停留时间
                            })
                            let newURL = ress.date[0].path;
                            let ad = that.data.images;
                            let am = ol;
                            ad.push(newURL);
                            console.log(ad);
                            console.log(newURL);
                            that.setData({
                                images: ad,
                                kk: kla
                            });
                        } else {
                            wx.showToast({
                                title: '未知错误',
                                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                duration: 2000
                            })
                        }

                    },
                    fail: function(resp) {
                        console.log(resp)
                    }
                }
                call.upImg(data);
            }
        })



    },
    test: function() {
        let kk = this.data.images;
        this.sendImg(kk);
    },
    // 图片预览
    previewImage: function(e) {
        //console.log(this.data.images);
        var current = e.target.dataset.src
        wx.previewImage({
            current: current,
            urls: this.data.images
        })
    },
    delete: function(e) {
        console.log(e)
        let that = this;
        wx.showModal({
            title: '删除图片',
            content: '确认要删除图片吗？',
            success: function(res) {
                if (res.confirm) {
                    var index = e.currentTarget.dataset.index;
                    var images = that.data.images;
                    console.log(images)
                    let pushURL = "Img/del?files=" + images[index];
                    let deleteData = {
                        URL: pushURL, //这里是你要上传的服务器接口
                        success: function(res) {
                            console.log(res);
                            console.log(images)
                            if (res.message == "删除成功") {
                                images.splice(index, 1);
                                that.setData({
                                    images: images,
                                    images2: images
                                });
                                ol = ol - 1;
                                if (ol != 3) {
                                    that.setData({
                                        kk: false
                                    })
                                }
                                wx.showToast({
                                    title: '删除成功',
                                    icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                    duration: 2000
                                })
                            } else {
                                wx.showToast({
                                    title: '删除失败',
                                    icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                    duration: 2000
                                })
                            }

                        },
                        fail: function(resp) {
                            wx.showToast({
                                title: '未知错误',
                                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                duration: 2000
                            })
                        }
                    }
                    call.deleteImg(deleteData);
                } else {
                    console.log('点击取消回调')
                }
            }
        })

    },
    up: function(pushData) {
        let data = {
            pushURL: "SearchPet/updateMySearchPet",
            pushData: pushData,
            success: function(res) {
                console.log(res) //发布成功后会执行此函数
                if (res.message === "操作成功") {
                    wx.hideLoading();
                    wx.showToast({
                        title: '提交成功',
                        icon: 'success', //如果要纯文本，不要icon，将值设为'none'
                        duration: 1000
                    })
                    setTimeout(() => {
                        wx.navigateBack({
                            delta: 1, // 回退前 delta(默认为1) 页面
                        })
                    }, 1000)
                } else {
                    wx.hideLoading();
                    console.log(res)
                    wx.showToast({
                        title: '提交失败,请重试！',
                        icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                        duration: 3000
                    })
                }
            },
            fail: function(resp) {
                console.length(resp);
                wx.showToast({
                    title: '出现未知错误',
                    icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                    duration: 3000
                })
            }
        }
        call.request4(data.pushURL, data.pushData, data.success, data.fail)

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        console.log("到底了")
    },
    admiin1: function() {

    },
    submit: function() {
        let hh = this;
        let pushData = "";
        pushData = this.make();
        wx.showModal({
            title: '提示',
            content: '确认是否提交',
            success: function(res) {
                if (res.confirm) {
                    if (hh.data.images.length != 0 && pushData.file_name != "" && pushData.search_pet_name != "" && pushData.search_pet_age != "" && pushData.search_pet_message != "" && pushData.publisher_phone != "" && pushData.lose_city_area != "" && pushData.lose_county_area != "" && pushData.lose_detailed_area != "") {
                        hh.up(pushData);
                        wx.showLoading({
                            title: '正在提交申请',
                        })
                    } else {
                        wx.showModal({
                            title: "温馨提示",
                            content: "请正确填写宠物信息并至少上传一张宠物图片",
                            success: function(res) {
                                if (res.confirm) {

                                } else if (res.cancel) {

                                }
                            }

                        })
                    }

                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },

    make: function() {
        let admin = this;
        let SearchM = {
            // "lose_detailed_area": "某街区某小区某栋楼",
            // "lose_time": "2019年9月9日",
            // lose_city_area: "福州",
            // lose_county_area: "鼓楼区",
            // lose_detailed_area: "百度新华街",
            // publisher_phone: "17687551157",
            // search_pet_age: "2月25天",
            // search_pet_category: "喵星人",
            // search_pet_message: "就是很吊？很吊就对了",
            // search_pet_name: "李晓静",
            // search_pet_sex: "女孩子",
            // search_pet_varieties: "埃及猫",
            // special_advice: "就是这么吊？？？？",
        };
        SearchM = admin.data.petmessage;
        if (admin.data.special_advice != undefined && admin.data.special_advice != "")
            SearchM.special_advice = admin.data.special_advice;
        if (admin.data.search_pet_name != undefined && admin.data.search_pet_name != "")
            SearchM.search_pet_name = admin.data.search_pet_name;
        if (admin.data.checkValue != undefined && admin.data.checkValue != "")
            SearchM.search_pet_category = admin.data.checkValue;
        if (admin.data.value != undefined && admin.data.value != "")
            SearchM.search_pet_varieties = admin.data.value;
        if (admin.data.search_pet_age != undefined && admin.data.search_pet_age != "")
            SearchM.search_pet_age = admin.data.search_pet_age;
        if (admin.data.checkValue_sex != undefined && admin.data.checkValue_sex != "")
            SearchM.search_pet_sex = admin.data.checkValue_sex;
        if (admin.data.lose_county_area != undefined && admin.data.lose_county_area != "")
            SearchM.lose_county_area = admin.data.lose_county_area;
        if (admin.data.search_pet_message != undefined && admin.data.search_pet_message != "")
            SearchM.search_pet_message = admin.data.search_pet_message;
        if (admin.data.lose_city_area != undefined && admin.data.lose_city_area != "")
            SearchM.lose_city_area = admin.data.lose_city_area;
        if (admin.data.lose_detailed_area != undefined && admin.data.lose_detailed_area != "")
            SearchM.lose_detailed_area = admin.data.lose_detailed_area;
        if (admin.data.dates != undefined && admin.data.dates != "" && admin.data.times != undefined && admin.data.times != "")
            SearchM.lose_time = admin.data.dates + " " + admin.data.times;
        if (admin.data.publisher_phone != undefined && admin.data.publisher_phone != "")
            SearchM.publisher_phone = admin.data.publisher_phone;
        console.log(SearchM)
        return SearchM;
        // // this.sendSearch(SearchM);
    },
    search_pet_name: function(e) {
        this.setData({
            search_pet_name: e.detail.value
        })
    },
    search_pet_age: function(e) {

        this.setData({
            search_pet_age: e.detail.value
        })
        console.log(this.data.search_pet_age)
    },
    // search_pet_contact: function (e) {
    //   this.setData({
    //     search_pet_contact: e.detail.value
    //   })
    // },
    search_pet_message: function(e) {
        this.setData({
            search_pet_message: e.detail.value
        })
    },
    search_pet_area: function(e) {
        this.setData({
            search_pet_area: e.detail.value
        })
    },
    publisher_phone: function(e) {
        this.setData({
            publisher_phone: e.detail.value
        })
    },
    // lose_time: function(e) {
    //     this.setData({
    //         lose_time: e.detail.value
    //     })
    // },
    // lose_city_area: function(e) {
    //     this.setData({
    //         lose_city_area: e.detail.value
    //     })
    // },
    // lose_county_area: function(e) {
    //     this.setData({
    //         lose_county_area: e.detail.value
    //     })
    // },
    special_advice: function(e) {
        this.setData({
            special_advice: e.detail.value
        })
        console.log(this.data.special_advice)
    },
    lose_detailed_area: function(e) {
        this.setData({
            lose_detailed_area: e.detail.value
        })
    }


    /**
     * 用户点击右上角分享
     */


})